﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;

 
    public class Text
    {
        public static string MD5(string s)
        {
            if (HttpContext.Current == null)
            {
                if (s == null || s.Length == 0)
                {
                    s = string.Empty;
                }
                return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower();
            }
            MD5 mD = System.Security.Cryptography.MD5.Create();
            byte[] array = HttpContext.Current.Request.ContentEncoding.GetBytes(s);
            array = mD.ComputeHash(array);
            return BitConverter.ToString(array).Replace("-", "").ToLower();
        }
        public static string MD5(string s, int start, int length)
        {
            return Text.MD5(s).Substring(start, length);
        }
        public static string MD5Confusion(string s)
        {
            string text = Text.MD5(s);
            string text2 = "";
            for (int i = 0; i < text.Length; i++)
            {
                char c = Convert.ToChar((int)(Convert.ToInt16(text[i]) + ((i > text.Length - i) ? 1 : -1)));
                if (i % 2 != 0)
                {
                    text2 = c.ToString() + text2;
                }
                else
                {
                    text2 += c.ToString();
                }
            }
            return text2;
        }
        public static string GenerateToken(string s)
        {
            if (s == null || s.Length == 0)
            {
                s = string.Empty;
            }
            return Text.MD5(s   );
        }
        public static bool ComparePassword(string pwd1, string pwd2)
        {
            if (pwd1 == null && pwd2 == null)
            {
                return true;
            }
            if (pwd1 == null || pwd2 == null)
            {
                return false;
            }
            int length = pwd1.Length;
            int length2 = pwd2.Length;
            if (length == length2 && length != 0)
            {
                return 0 == string.Compare(pwd1, pwd2, true);
            }
            if (32 == length && 16 == length2)
            {
                return 0 == string.Compare(pwd1.Substring(8, 16), pwd2, true);
            }
            if (16 == length)
            {
                if (32 == length2)
                {
                    return 0 == string.Compare(pwd2.Substring(8, 16), pwd1, true);
                }
            }
            return false;
        }
        public static string AddZero(int i)
        {
            return ((i > 9) ? string.Empty : "0") + i;
        }
        public static string SqlEncode(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            string[] array = "'|/\\*\\*/|exec|insert|select|delete|update|truncate".Split(new char[]
			{
				'|'
			});
            string[] array2 = "''|//|ex&#101;c|ins&#101;rt|sel&#101;ct|del&#101;te|up&#100;ate|truncat&#101;".Split(new char[]
			{
				'|'
			});
            try
            {
                RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline;
                for (int i = 0; i < array.Length; i++)
                {
                    Regex regex = new Regex(string.Format("{0}", array[i]), options);
                    s = regex.Replace(s, array2[i]);
                }
            }
            catch (Exception ex)
            {
                s = string.Format("SQL注入检查出错了：", ex.Message);
            }
            return s;
        }
        public static string ShitEncode(string s)
        {
            string text = "";// ConfigHelper.Get("BadWords");
            if (text != null && text.Length != 0)
            {
                text = Regex.Replace(text, "\\|{2,}", "|");
                text = Regex.Replace(text, "(^\\|)|(\\|$)", string.Empty);
            }
            else
            {
                text = "妈的|你妈|他妈|妈b|妈比|fuck|shit|我日|法轮|我操";
            }
            return Regex.Replace(s, text, "**", RegexOptions.IgnoreCase);
        }
        public static string TextEncode(string s)
        {
            if (s != null && s.Length != 0)
            {
                StringBuilder stringBuilder = new StringBuilder(s);
                stringBuilder.Replace("&", "&amp;");
                stringBuilder.Replace("<", "&lt;");
                stringBuilder.Replace(">", "&gt;");
                stringBuilder.Replace("\"", "&quot;");
                stringBuilder.Replace("'", "&#39;");
                return Text.ShitEncode(stringBuilder.ToString());
            }
            return string.Empty;
        }
        public static string TextDecode(string s)
        {
            StringBuilder stringBuilder = new StringBuilder(s);
            stringBuilder.Replace("<br/><br/>", "\r\n");
            stringBuilder.Replace("<br/>", "\r");
            stringBuilder.Replace("<p></p>", "\r\n\r\n");
            return stringBuilder.ToString();
        }
        public static string HtmlEncode(string s)
        {
            return Text.HtmlEncode(s, true);
        }
        public static string HtmlEncode(string s, bool bln)
        {
            if (string.IsNullOrEmpty(s))
            {
                return s;
            }
            StringBuilder stringBuilder = new StringBuilder(s);
            stringBuilder.Replace("&", "&amp;");
            stringBuilder.Replace("<", "&lt;");
            stringBuilder.Replace(">", "&gt;");
            stringBuilder.Replace("\"", "&quot;");
            stringBuilder.Replace("'", "&#39;");
            stringBuilder.Replace("\r\n", "<br/>");
            stringBuilder.Replace("\r", "<br/>");
            stringBuilder.Replace("\n", "<br/>");
            if (bln)
            {
                return Text.ShitEncode(stringBuilder.ToString());
            }
            return stringBuilder.ToString();
        }
        public static string HtmlDecode(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return s;
            }
            StringBuilder stringBuilder = new StringBuilder(s);
            stringBuilder.Replace("&amp;", "&");
            stringBuilder.Replace("&lt;", "<");
            stringBuilder.Replace("&gt;", ">");
            stringBuilder.Replace("&quot;", "\"");
            stringBuilder.Replace("&#39;", "'");
            stringBuilder.Replace("<br/>", "\r\n");
            return stringBuilder.ToString();
        }
        public static int Len(string s)
        {
            return Encoding.Default.GetByteCount(s);
        }
        public static string Left(string s, int need, bool encode)
        {
            return Text.Left(s, need, encode, "...");
        }
        public static string Left(string s, int need, bool encode, string endStr)
        {
            //if (s == null || s == "")
            //{
            //    return string.Empty;
            //}
            //int length = s.Length;
            //if (length > need / 2)
            //{
            //    int num = 0;
            //    for (int i = 0; i < length; i++)
            //    {
            //        num += (RegExp.IsUnicode(s[i].ToString()) ? 2 : 1);
            //        if (num >= need)
            //        {
            //            if (num == need)
            //            {
            //                i++;
            //            }
            //        IL_63:
            //            string text = s.Substring(0, i);
            //            return ((!encode) ? text : Text.TextEncode(text)) + endStr;
            //        }
            //    }
            //    goto IL_63;
            //}
            //if (encode)
            //{
            //    return Text.TextEncode(s);
            //}
            return s;
        }
        public static string EmailEncode(string s)
        {
            string text = Text.TextEncode(s).Replace("@", "&#64;").Replace(".", "&#46;");
            return Text.JoinString(new string[]
			{
				"<a href='mailto:",
				text,
				"'>",
				text,
				"</a>"
			});
        }
        public static string JoinString(params string[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (value.Length == 0)
            {
                return string.Empty;
            }
            return string.Join(string.Empty, value);
        }
        public static bool IsNumberList(string str)
        {
            return Text.IsNumberList(str, ',');
        }
        public static bool IsNumberList(string str, char separator)
        {
            if (str == null)
            {
                return false;
            }
            int num = str.Length;
            if (num == 0)
            {
                return false;
            }
            if (char.IsNumber(str[0]) && char.IsNumber(str[num - 1]))
            {
                num--;
                for (int i = 1; i < num; i++)
                {
                    if (separator != str[i])
                    {
                        if (!char.IsNumber(str[i]))
                        {
                            return false;
                        }
                    }
                    else
                    {
                        if (!char.IsNumber(str[i - 1]) || !char.IsNumber(str[i + 1]))
                        {
                            return false;
                        }
                    }
                }
                return true;
            }
            return false;
        }
        public static bool IsNumeric(object value)
        {
            return value != null && Text.IsNumeric(value.ToString());
        }
        public static bool IsNumeric(string value)
        {
            if (value == null)
            {
                return false;
            }
            int length = value.Length;
            if (length == 0)
            {
                return false;
            }
            if ('-' != value[0] && !char.IsNumber(value[0]))
            {
                return false;
            }
            for (int i = 1; i < length; i++)
            {
                if (!char.IsNumber(value[i]))
                {
                    return false;
                }
            }
            return true;
        }
        public static string JavaScriptEncode(string str)
        {
            StringBuilder stringBuilder = new StringBuilder(str);
            stringBuilder.Replace("\\", "\\\\");
            stringBuilder.Replace("\r", "\\0Dx");
            stringBuilder.Replace("\n", "\\x0A");
            stringBuilder.Replace("\"", "\\x22");
            stringBuilder.Replace("'", "\\x27");
            return stringBuilder.ToString();
        }
        public static string JavaScriptEncode(object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            return Text.JavaScriptEncode(obj.ToString());
        }
        public static string LoseHtml(string htmlCode)
        {
            if (htmlCode != null && htmlCode.Length != 0)
            {
                return Regex.Replace(htmlCode, "<[^>]+>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);
            }
            return string.Empty;
        }
        public static string[] GetStrArray(string str)
        {
            return Text.GetStrArray(str, ',');
        }
        public static string[] GetStrArray(string str, char strchar)
        {
            return str.Split(new char[]
			{
				strchar
			});
        }
        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    stringBuilder.Append(list[i]);
                }
                else
                {
                    stringBuilder.Append(list[i]);
                    stringBuilder.Append(speater);
                }
            }
            return stringBuilder.ToString();
        }
        public static string[] Split(string str, string splitStr)
        {
            return str.Split(new string[]
			{
				splitStr
			}, StringSplitOptions.RemoveEmptyEntries);
        }
        public static string DelLastComma(string str)
        {
            return Text.DelLastChar(str, ",");
        }
        public static string DelLastChar(string str, string strchar)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return str.Substring(0, str.LastIndexOf(strchar));
            }
            return "";
        }
        public static bool IsInString(string arr, string str)
        {
            return !string.IsNullOrEmpty(arr) && !string.IsNullOrEmpty(str) && ("_" + arr).IndexOf(str) > 0;
        }
        public static string ToSBC(string input)
        {
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == ' ')
                {
                    array[i] = '\u3000';
                }
                else
                {
                    if (array[i] < '\u007f')
                    {
                        array[i] += 'ﻠ';
                    }
                }
            }
            return new string(array);
        }
        public static string ToDBC(string input)
        {
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != '\u3000')
                {
                    if (array[i] > '＀' && array[i] < '｟')
                    {
                        array[i] -= 'ﻠ';
                    }
                }
                else
                {
                    array[i] = ' ';
                }
            }
            return new string(array);
        }
    }
 
